package com.inspur.gs.sbssdl.sbwhgl.sbrh.sbrh.back.luCreatFactory;

import com.inspur.edp.qdp.bql.api.IBqlExecuter;
import io.iec.edp.caf.runtime.config.BqlBeanUtil;

import com.cronutils.model.Cron;
import com.cronutils.model.CronType;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;
import lombok.Getter;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class LuTimeUtil {

    private final IBqlExecuter bqlExecuter = BqlBeanUtil.getAppCtx().getBean(IBqlExecuter.class);

    private final String rhrwCfg = "com.inspur.gs.sbssdl.sbwhgl.sbrh.sbrh.back.sbssSbrhRw";
    @Getter
    public DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public List<String> getStartAndEndTimerByCron(String cycleLabel, int cycleValue) {
        ArrayList<String> times = new ArrayList<>();
        ZonedDateTime time = ZonedDateTime.now();
        ZonedDateTime start;
        ZonedDateTime end;

        String cronStr;
        switch (cycleLabel) {
            case "1":
                // 时
                cronStr = "0 0 * * * *";
                start = time.minusHours(1);
                end = time.plusHours(cycleValue + 1);
                break;
            case "2":
                // 天
                cronStr = "0 0 0 1/" + cycleValue + " * *";
                start = time.minusDays(cycleValue);
                end = time.plusDays(cycleValue);
                if (cycleValue > 1) {
                    cycleValue = 1;
                }
                break;
            case "3":
                // 周
                cronStr = "0 0 0 * * 1";
                start = time.minusDays(7);
                end = time.plusDays(cycleValue * 7L + 1);
                break;
            case "4":
                // 月
                cronStr = "0 0 0 1/31 1/" + cycleValue + " *";
                start = time.minusMonths((cycleValue));
                end = time.plusMonths(cycleValue);
                if (cycleValue > 1) {
                    cycleValue = 1;
                }
                break;
            case "5":
                // 季
                cronStr = "0 0 0 1 1/3 *";
                start = time.minusMonths(3);
                end = time.plusMonths(cycleValue * 3L + 1);
                break;
            case "6":
                // 年
                cronStr = "0 0 0 1 1 *";
                start = time.minusYears(1);
                end = time.plusYears(cycleValue + 1);
                break;
            default:
                return null;
        }

        CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.SPRING));
        ExecutionTime executionTime;
        try {
            Cron cron = parser.parse(cronStr);
            cron.validate();
            executionTime = ExecutionTime.forCron(cron);
        } catch (Exception e) {
            System.out.println("cron表达式错误" + cronStr);
            return null;
        }

        for (int i = 0; i < cycleValue + 1; i++) {
            Optional<ZonedDateTime> next = executionTime.nextExecution(start);
            if (next.isPresent() && !next.get().isEqual(start) && next.get().isBefore(end)) {
                start = next.get();
            } else {
                break;
            }

            if (i == 0 || i == cycleValue) {
                times.add(start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }
        }

        return times;
    }

    public List<String> getStartAndEndTimer(String cycleLabel, int cycleValue) {
        ArrayList<String> times = new ArrayList<>();
        ZonedDateTime start = ZonedDateTime.now();
        ZonedDateTime end = ZonedDateTime.now();

        DateTimeFormatter ofPattern;
        switch (cycleLabel) {
            case "1":
                // 时
                end = start.plusHours(cycleValue);
                ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00");
                times.add(start.format(ofPattern));
                times.add(end.format(ofPattern));
                break;
            case "2":
                // 天
                end = start.plusDays(cycleValue);
                ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00");
                times.add(start.format(ofPattern));
                times.add(end.format(ofPattern));
                break;
            case "3":
                // 周
                return getStartAndEndTimerByCron(cycleLabel, cycleValue);
            case "4":
                // 月
                end = start.plusMonths(cycleValue);
                ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-01 00:00:00");
                times.add(start.format(ofPattern));
                times.add(end.format(ofPattern));
                break;
            case "5":
                // 季
                return getStartAndEndTimerByCron(cycleLabel, cycleValue);
            default:
                // 年
                end = start.plusYears(cycleValue);
                ofPattern = DateTimeFormatter.ofPattern("yyyy-01-01 00:00:00");
                times.add(start.format(ofPattern));
                times.add(end.format(ofPattern));
        }
        return times;
    }
}
